﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Files
{
    public static class FilesController
    {
        public static string ReadFile(string _filePath, string _fileName)
        {
            string strLine = string.Empty;
            try
            {
                if (!Directory.Exists(_filePath))
                {
                    Directory.CreateDirectory(_filePath);
                }

                _filePath = Path.Combine(_filePath, _fileName);
                if (!File.Exists(_filePath))
                {
                    FileStream f = File.Create(_filePath);
                    f.Close();
                    f.Dispose();
                    goto StepResult;
                }

                TextReader file = new StreamReader(_filePath);
                strLine = file.ReadToEnd();
                file.Close();
                file.Dispose();

                if (string.IsNullOrEmpty(strLine))
                    strLine = string.Empty;

                strLine.Replace("\r\n", "").Replace("\t", "").Trim();
            }
            catch
            {
            }

        StepResult:
            return strLine;
        }

        public static bool WriteFile(string _text, string _filePath, string _fileName)
        {
            bool blResult = false;
            try
            {
                if (!Directory.Exists(_filePath))
                {
                    Directory.CreateDirectory(_filePath);
                }

                _filePath = Path.Combine(_filePath, _fileName);
                if (!File.Exists(_filePath))
                {
                    FileStream f = File.Create(_filePath);
                    f.Close();
                    f.Dispose();
                    goto StepResult;
                }

                TextWriter file = new StreamWriter(_filePath);
                file.Write(_text);
                file.Close();
                file.Dispose();
            }
            catch
            {
            }
        StepResult:
            return blResult;
        }
    }
}
